package trefliq.EJB.sessionbeans;

import java.util.List;
import javax.annotation.security.PermitAll;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import trefliq.EJB.entities.Employee;
import trefliq.EJB.entities.Movie;

/**
 *
 * @author Brandon
 */
@Stateless
@LocalBean
@PermitAll // (by default, allow all, restrict what's necessary)
public class EmployeeSession {
    @EJB
    MovieBean movieBean;
    
   @PersistenceContext(unitName="trefliqPU")
   private EntityManager em;
   
   //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
   public Employee createEmployee(Employee e) { 
       em.persist(e);
       return e; 
   }
   
   //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
   public Employee deleteEmployee(Employee e) { 
       Employee emp = em.find(Employee.class, e.getId()); 
       em.remove(emp);
       return emp;
   }
   
   //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
   public List<Employee> getAllEmployees() {
       return em.createNamedQuery("findAllEmployees", Employee.class).getResultList();
   }
   
   //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
   public Employee getEmployeeById(Long employeeId) { 
        return em.createNamedQuery("findEmployeeById", Employee.class).
                    setParameter("id", employeeId).getSingleResult();
       
   }
   
   //TODOSEC: @RolesAllowed({SecurityRole.ADMIN})
   public Employee getEmployeeByPersonId(Long personId) { 
       return em.createNamedQuery("findEmployeeByPersonId", Employee.class).
                    setParameter("personId", personId).getSingleResult();
   }
   
   //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
   public boolean addMovie(Movie movie) { 
       boolean success = false;
       Movie newMovie = movieBean.CreateMovie(movie);
       if(movie.getTitle().contentEquals(movie.getTitle())) {
           success = true;
       }
       return success;
   }
  
   //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
   public boolean updateMovie(Movie movie) { 
       boolean success = movieBean.UpdateMovie(movie);
       return success;
   }

   //TODOSEC: @RolesAllowed({SecurityRole.ADMIN, SecurityRole.EMPLOYEE})
   public boolean deleteMovie(Movie movie) { 
       boolean success = movieBean.DeleteMovie(movie);
       return success;
   }

}
